<?php
/*
	我的购物车
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['lang']){
	$_SESSION['show_lang']=$_RQ['lang'];
}
//未登录下单
$return['ischecklogin']=model('jy_weishop/member')->checklogin('','','','',true);
$return['item']=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
if(!$return['item'] && !$_RQ['isstore']){
	$_SESSION['uid']=0;
	//exi('找不到会员信息！请先登陆！','error','jy_weishop/account.login/');
}
$store_id=$_SESSION['store_id'];
if($_RQ['op']=='main'){
	//$return['setting']=$this->plugin_setting('basic');
	$return['list']=array();
	$return['params']=array();
	$return['cartnums']= pdo_getsum('jy_weishop_member_cart',array('mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
	set_html('title','配置清单');
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$ismerch=intval($_RQ['ismerch']);
	if($_RQ['store_id']){
		$_SESSION['store_id']=intval($_RQ['store_id']);
		$store_id=$_SESSION['store_id'];
	}
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'pluginname'=>'jy_kongtiaoshop',
	);
	//未登录下单
	if(!$_SESSION['uid']){
		$return['list']=$_SESSION['cartlist']?:array();
		exi($return);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 100;
	$total='';
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=pdo_getlist('jy_weishop_member_cart',$parameter,$total);
	if($list){
		foreach($list as $k=>&$item){
			$goodsfile=array('id','isendtime','endtime','status','fang_credit2','no_de_sales','isdiyprice','diyprice','invoice','type','buylevels','buygroups','title','merchid','status','pcate','ccate','total','subtitle','image','video','price','totalcnf','weight','active_plugin','active_id','storeids','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit','isverify','diyfields','api_id','api_pluginname','api_data');
			$goods=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid'],'uniacid'=>$_SESSION['uniacid']),$goodsfile);
			if(!$goods['id']){
				pdo_delete('jy_weishop_member_cart',array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				unset($list[$k]);
				continue;
			}
			unset($item['storeid']);
			if($_SESSION['is_tomedia']){
				$goods['image']=$goods['image']?tomedia($goods['image']):'';
			}
			$item['merchid']=$goods['merchid'];
			//获取活动信息
			$goods['storeid']=$store_id;
			$goods['optionid']=$item['optionid'];
			$goods=model('jy_weishop/activity')->get_upgoods($goods);
			$item['oldprice']=$goods['oldprice'];
			$item['price']=$goods['price'];
			$item['totalprice']=$item['total']*$goods['price'];
			if($feemoneyset['isopen']==1){
				$item['feemoney']=$goods['marketprice']>0?$goods['marketprice']-$goods['price']:$feemoneyset['feerade']*$goods['price']/100;
				$item['feemoney']=price($item['feemoney']);
			}
			$goods['userbuy'] = 1;
			$goods['levelbuy'] = 1;
			$goods['groupbuy'] = 1;
			$goods['canbuy'] = ($goods['status'] == 1) && ($goods['total'] > 0);
			if (0 < $goods['usermaxbuy']) {
				$order_goodscount = pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'goodsid' => $goods['id'],'uniacid' => $_SESSION['uniacid'],'mid' => $_SESSION['uid']),'total');
				$isselect=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'optionid <>'=>$item['optionid'],'goodsid' => $goods['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'total');
				if ($goods['usermaxbuy'] <= $order_goodscount + $isselect) {
					$goods['userbuy'] = 0;
					$goods['canbuy'] = false;
					pdo_update('jy_weishop_member_cart',array('selected'=>2),array('id'=>$item['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
				}
				$goods['order_goodscount']=$order_goodscount;
			}
			if ((0 < $goods['isendtime']) && (0 < $goods['endtime']) && ($goods['endtime'] < time())) {
				$goods['canbuy'] = false;
			}
			$item['kucun']=$goods['total'];
			if($item['optionid']){
				$item['option']=$goods['options'];
				$item['kucun']=$item['option']['stock'];
				if(!$goods['options']['specs']){
					$optiontitle=$goods['options']['title'];
					$goods['istaocan'] = 1;
				}else{
					$specs = explode("_", $item['option']['specs']);
					$specsall = pdo_getall('jy_weishop_goods_spec_item',array('id'=>$specs,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
					if($specsall){
						$optiontitle='';
						foreach($specsall as $k=>&$sp){
							if($k==0){
								$optiontitle .=$sp['title'];
							}else{
								$optiontitle .='，'.$sp['title'];
							}
						}
					}
				}
				$item['optiontitle']= $optiontitle;
				if($item['option']['stock']<1){
					$goods['canbuy'] = false;
				}
			}
			//判断nobuy状态：
			$unit=$goods['unit']?:'件';
			if($goods['userbuy']==0){
				$goods['nobuytitle']='已超出最大'.$goods['usermaxbuy'].'件购买量';
			}elseif($goods['levelbuy']==0){
				$goods['nobuytitle']='您当前会员等级没有购买权限';
			}elseif($goods['groupbuy']==0){
				$goods['nobuytitle']='您所在的用户组没有购买权限';
			}elseif($goods['timebuy']==-1){
				$goods['nobuytitle']='未到开始抢购时间!';
			}elseif($goods['timebuy']==1){
				$goods['nobuytitle']='抢购时间已经结束';
			}else{
				$goods['nobuytitle']='商品已售罄';
			}
			if ($goods['usermaxbuy'] && $goods['usermaxbuy'] < $order_goodscount + $item['total']) {
				$goods['outnumtitle']='超购买量,请减少';
				$goods['outtype']=1;
			}
			if($item['kucun']<$item['total']){
				$goods['outnumtitle']='超库存,请减少';
				$goods['outtype']=2;
			}
			if($goods['minbuynum'] && $item['total']<$goods['minbuynum']){
				$goods['outnumtitle']='单次最少购买'.$goods['minbuynum'].$unit;
				$goods['outtype']=3;
			}
			if($goods['maxbuy'] && $item['total']>$goods['maxbuy']){
				$goods['outnumtitle']='单次最多购买'.$goods['minbuynum'].$unit;
				$goods['outtype']=4;
			}
			$sales=model('jy_weishop/activity')->get_sales($goods['id']);
			if($sales['maxnum']>0 && $item['total']>$sales['maxnum']){
				$goods['outnumtitle']='只能购买活动品'.$sales['maxnum'].$unit;
				$goods['outtype']=5;
			}
			if($goods['outtype'] || !$goods['canbuy']){
				pdo_update('jy_weishop_member_cart',array('selected'=>0),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
				$item['selected']=0;
			}
			$goods['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$item['goodsid']),'weight');
			unset($goods['total']);
			$goods['api_data']=json_decode($goods['api_data'],true);
			$item=array_merge($goods,$item);
		}
	}
	$list=array_values($list);
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='plus'){
	//增加
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
	$cart=pdo_get('jy_weishop_member_cart',array('id'=>$id),array('id','total','goodsid','optionid'));
	model('jy_weishop/goods')->check_buy($cart['goodsid'],intval($cart['total'])+1,intval($cart['optionid']),true,true);
	if($cart['optionid']){
		model('jy_weishop/cart')->check_select($cart);
	}
	pdo_update('jy_weishop_member_cart',array('total +='=>1),array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	exi('success');
}
if($_RQ['op']=='minus'){
	//减少
	$post=get_postdata();
	$id=intval($post['id']);
	$goodsid=intval($post['goodsid']);
	$isnotgoodsid=intval($_RQ['isnotgoodsid']);
	$optionid=intval($post['optionid']);
	$merchid=intval($post['merchid']);
	if(!$id && !$goodsid){
		exi('请先选择商品！','error');
	}
	if($id){
		$check=pdo_get('jy_weishop_member_cart',array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('total','goodsid','optionid'));
	}elseif($goodsid){
		$check=pdo_get('jy_weishop_member_cart',array('optionid'=>$optionid,'goodsid'=>$goodsid,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','total','goodsid','optionid'));
		$id=$check['id'];
	}
	if(!$check){
		//return false;
		exi('指定商品不存在！'.$id,'error');
	}
	if($check['total']<=1){
		pdo_delete('jy_weishop_member_cart',array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($post['getlist']){
		}elseif($post['wxapp']){
			exi('success');
		}else{
			exi('删除成功!');
		}	
	}elseif($check){
		//减少时不核实，这样可以清空购物车
		pdo_update('jy_weishop_member_cart',array('total -='=>1),array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($post['getlist']){
		}else{
			exi('success');
		}
	}
	$allcart=model('jy_weishop/cart')->getall('',$merchid,$isnotgoodsid);
	//运费计算：
	if($merchid){
		$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
	}
	$order=array(
		'list'=>$allcart['list'],
		'total_price'=>$allcart['goodsprice'],
	);
	if(!$merchid){
		$up_dispatch=model('jy_weishop/order/dispatch')->up_dispatch($order,array(),'',$_SESSION['store_id'],$merch);
	}
	$res['dispatchprice']=$up_dispatch['get_freight']['fee']?:0;
	$res['list']=$allcart['list'];
	exi($res);
}
if($_RQ['op']=='addtocart'){
	$post=get_postdata();
	$id=intval($post['goodsid']);
	$merchid=intval($post['merchid']);
	$number=intval($post['num']);
	$isnotgoodsid=intval($_RQ['isnotgoodsid']);
	if(!$id){
		exi('选择的商品不存在，请重新选择！','error');
	}
	model('jy_weishop/activity/sales')->check_sale_new(array(array('goodsid'=>$id)));
	if(!$number){
		$number=1;
	}
	$spsids = '';
	$post['optionid']=$post['optionid']?:$post['optionsid'];
	if($post['specid']){
		foreach($post['specid'] as $k=>&$s){
			if($k==0){
				$spsids .= $s;
			}else{
				$spsids .= '_'.$s;
			}
		}
		$optionid = pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$spsids),'id');
	}
	if(!$optionid && $post['optionid']){
		$optionid=$post['optionid'];
	}
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($store_id){
				$getinfo=model('jy_weishop/store')->getinfo($id,$store_id);
				$goods['total']=$getinfo['id']?$getinfo['total']:$goods['total'];
				$goods['price']=$getinfo['id']?$getinfo['price']:$goods['price'];
				if($optionid){
					$options=model('jy_weishop/store')->getoption($id,$store_id,'',$optionid);
				}
	}
	if(!$options['id'] && $optionid){
		$options = pdo_get('jy_weishop_goods_option',array('goodsid'=>$id,'id'=>$optionid));
	}
	$ifgoods=pdo_get('jy_weishop_member_cart',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id,'optionid'=>$optionid));
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'goodsid'=>$id,
		'price'=>$options['price']>0?$options['price']:$goods['price'],
		'optionid'=>$optionid,
		'selected'=>1,
	);
	if($ifgoods){
		$cartid=$ifgoods['id'];
		$data['total']=$ifgoods['total']+$number;
		model('jy_weishop/goods')->check_buy($data['goodsid'],intval($data['total']),intval($data['optionid']),true,true);
		pdo_update('jy_weishop_member_cart',$data,array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$ifgoods['id']));
		if($post['getlist']){
		}else{
			exi('success');
		}
	}else{
		$data['total']=$number;
		$data['storeid']=$store_id;
		model('jy_weishop/goods')->check_buy($data['goodsid'],intval($data['total']),intval($data['optionid']),true,true);
		//如果来自其他商户分销（保存商户ID）
		$data['merchid']=$goods['merchid']?:$this->session('frommerchid');
		$data['pluginname']=$post['pluginname']?:'';
		pdo_insert('jy_weishop_member_cart',$data);
		$cartid=pdo_insertid();
	}
	$allcart=model('jy_weishop/cart')->getall('',$merchid,$isnotgoodsid);
	//运费计算：
	if($merchid){
		$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
	}
	$order=array(
		'list'=>$allcart['list'],
		'total_price'=>$allcart['goodsprice'],
	);
	if(!$merchid){
		$up_dispatch=model('jy_weishop/order/dispatch')->up_dispatch($order,array(),'',$_SESSION['store_id'],$merch);
	}
	$res['dispatchprice']=$up_dispatch['get_freight']['fee']?:0;
	$res['list']=$allcart['list'];
	$res['cartid']=$res['id']=$cartid;
	exi($res);
}
if($_RQ['op']=='check_buy'){
		$post=get_postdata();
		if(!$post['name'] || !$post['mobile'] || !$post['pcd'] ||  !$post['address']){
			exi('必须填写姓名，电话，地址等','error');
		}
		$mid=$_SESSION['uid'];
		$address=array(
			'mobile'=>trim($post['mobile']),
			'name'=>trim($post['name']),
			'pcd'=>$post['pcd'],
			'address'=>trim($post['address']),
		);
		$price=0;
		$goodslist=array();
		$params=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'pluginname'=>'jy_kongtiaoshop',
		);
		$cartlist=pdo_getall('jy_weishop_member_cart',$params);
		$post['goods']=$post['kongjian']=array();
		foreach($cartlist as $cv){
			$cv['no_onebuy']=pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),'no_onebuy');
			if($cv['no_onebuy']==1 || $cv['no_onebuy']==0){
				$post['goods'][]=$cv;
			}elseif($cv['no_onebuy']==2){
				$post['kongjian'][]=$cv;
			}elseif($cv['no_onebuy']==3){
				$post['waijiid']=$gv['goodsid'];
			}
		}
		foreach($post['goods'] as &$gv){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']));
			$gv['total']=1;
			$gv['price']=$goods['price'];
			$gv['plugin_name']='jy_kongtiaoshop';
			$goodslist[]=$gv;
			$price +=$goods['price'];
		}
		foreach($post['kongjian'] as &$gv){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']));
			$gv['total']=1;
			$gv['price']=$goods['price'];
			$gv['plugin_name']='jy_kongtiaoshop';
			$goodslist[]=$gv;
			$price +=$goods['price'];
		}
		if($post['waijiid']){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$post['waijiid'],'uniacid'=>$_SESSION['uniacid']));
			$goodslist[]=array(
				'goodsid'=>$post['waijiid'],
				'total'=>1,
				'price'=>$goods['price'],
				'plugin_name'=>'jy_kongtiaoshop',
			);
		}
		$price +=$goods['price'];
		$post['peiinfo']['goods']=$post['goods'];
		$post['peiinfo']['kongjian']=$post['kongjian'];
		$post['peiinfo']['waijiid']=$post['waijiid'];
		$api_data=array(
			'peiinfo'=>$post['peiinfo'],
			'type'=>2,
			'storeid'=>$post['storeid'],
			'shequid'=>$post['shequid'],
			'homeid'=>$post['homeid'],
			'peilistid'=>$post['peilistid'],
			'hometypeid'=>$post['hometypeid'],
		);
		$api_data=array_merge($post['api_data'],$api_data);
		$new_order=array(
			'mid'=>$_SESSION['uid'],
			'price'=>$price,
			'goodsprice'=>$price,
			'goods'=>$goodslist,
			'realname'=>$post['name'],
			'mobile'=>$post['mobile'],
			'address'=>$address,
			'is_peisong'=>1,
			'sendtype'=>1,
			'type'=>1,
			'storeid'=>$post['storeid'],
			'plugin_name'=>'jy_kongtiaoshop',
			'api_data'=>ijson_encode($api_data),
		);
		if($post['orderid']){
			foreach($goodslist as $k=>$gv){
				$order_goods=pdo_get('jy_weishop_order_goods',array('orderid'=>$post['orderid'],'goodsid'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']));
				if($order_goods['id']){
					unset($goodslist[$k]);
				}
			}
			$new_order['goods']=$goodslist;
			$new_order['oldorderid']=$post['orderid'];
			model('jy_weishop/order')->create($new_order);
			pdo_update('jy_weishop_order',array('api_data'=>$new_order['api_data']),array('id'=>$post['orderid'],'uniacid'=>$_SESSION['uniacid']));
			unset($_SESSION['postdata']);
			exi('更新配置单成功！','','/order.list/');
		}else{
			model('jy_weishop/order')->create($new_order);
			unset($_SESSION['postdata']);
			exi('生成配置单成功！','','/order.list/');
		}
	exi('生成配置单成功！','','/order.list/');
}
if($_RQ['op']=='selectall'){
	$type=intval($_RQ['type']);
	pdo_update('jy_weishop_member_cart',array('selected'=>$type),array('selected'=>array(0,1),'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	exi('success');
}
if($_RQ['op']=='select'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
	$check=pdo_get('jy_weishop_member_cart',array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','total','goodsid','optionid'));
	if(!$check){
		exi('指定商品不存在！','error');
	}
	if($post['selected'] && $check['optionid']){
		model('jy_weishop/cart')->check_select($check);
	}
	pdo_update('jy_weishop_member_cart',array('selected'=>intval($post['selected'])),array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	exi('success');
}
if($_RQ['op']=='selectmerch'){
	$post=get_postdata();
	if($post['id']==-1){
        $post['id']=0;
    }
	$id=intval($post['id']);
	$selectmerch=$this->session('selectmerch')?:array('0'=>1);
	if($_RQ['isnew']){
        $selectmerch[$id]=intval($post['selected']);
    }else{
        $selectmerch[$id]= $selectmerch[$id]?0:1;
    }
	$this->session('selectmerch',$selectmerch);
    pdo_update('jy_weishop_member_cart',array('selected'=>$selectmerch[$id]),array('merchid'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	exi('success');
}
if($_RQ['op']=='offeradd'){
	$sale_offer=model('jy_weishop/activity/sales')->getoffer();
	foreach($sale_offer['list'] as $item){
		if($item['canbuy']){
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'goodsid'=>$item['id'],
				'price'=>$item['oldprice'],
				'optionid'=>$item['optionsid'],
				'selected'=>1,
			);
			$ifgoods=pdo_get('jy_weishop_member_cart',array('goodsid'=>$item['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','total'));
			if($ifgoods){
				$data['total']=$ifgoods['total']+1;
				pdo_update('jy_weishop_member_cart',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$ifgoods['id']));
			}else{
				$data['total']=1;
				//如果来自其他商户分销（保存商户ID）
				$data['total']['merchid']=$this->session('frommerchid');
				pdo_insert('jy_weishop_member_cart',$data);
			}
		}
	}
	exi('添加成功！');
}
if($_RQ['op']=='newadd'){
	$sale_new=model('jy_weishop/activity/sales')->getnew();
	foreach($sale_new['list'] as $item){
		if($item['cant_buy']!=1){
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'goodsid'=>$item['id'],
				'price'=>$item['price'],
				'optionid'=>$item['optionsid'],
				'selected'=>1,
			);
			$ifgoods=pdo_get('jy_weishop_member_cart',array('goodsid'=>$item['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','total'));
			if($ifgoods){
				$data['total']=$ifgoods['total']+1;
				pdo_update('jy_weishop_member_cart',$data,array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$ifgoods['id']));
			}else{
				$data['total']=1;
				//如果来自其他商户分销（保存商户ID）
				$data['total']['merchid']=$this->session('frommerchid');
				pdo_insert('jy_weishop_member_cart',$data);
			}
		}
	}
	exi('添加成功！');
}
if($_RQ['op']=='upcart'){
	$post=get_postdata();
	foreach($post as $k=>$total){
		if($total>0){
			pdo_update('jy_weishop_member_cart',array('total'=>$total),array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$k));
		}
	}
	exi('success');
}
if($_RQ['op']=='del'){
	//删除
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	if(count($del)==0){
		exi('请先选择商品','error');
	}
	foreach($del as $id){
		$id=intval($id);
		pdo_delete('jy_weishop_member_cart',array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	}
	exi('删除成功！');
}
?>